<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" class="login-form" label-position="left" :rules="loginFormRules">
      <img src="../../assets/img/logo.595745bd.png" alt="" class="login-logo">
      <!-- <div class="title-container" /> -->
      <el-form-item prop="loginName">
        <span class="svg-container el-icon-mobile-phone" />
        <el-input v-model="loginForm.loginName" placeholder="请输入账号" />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container el-icon-lock" />
        <el-input ref="pwdInput" v-model="loginForm.password" placeholder="请输入密码" :type="passwordType" />
        <span class="svg-container" @click="change">
          <svg-icon :icon-class="`${passwordType==='password'?'eye':'eye-open'}`" />
        </span>
      </el-form-item>
      <el-form-item prop="code">
        <span class="svg-container el-icon-lock" />
        <el-input v-model="loginForm.code" placeholder="请输入验证码" />
      </el-form-item>
      <el-button type="primary" style="width:100%;margin-bottom:30px;" class="login-btn" :loading="loading" @click="loginBtn">登录</el-button>
      <el-col :span="7" class="authCode">
        <img
          :src="`https://likede2-java.itheima.net/api/user-service/user/imageCode/${loginForm.clientToken}`"
          style="width:100%;height:100%"
          @click="loginForm.clientToken = Math.random()"
        >
      </el-col>
      <div class="tips">
        <span style="margin-right:20px;">username: admin</span>
        <span> password: any</span>
      </div>

    </el-form>
  </div>
</template>

<script>
// import { validUsername } from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    return {
      passwordType: 'password',
      loading: false,
      loginForm: {
        loginName: 'admin',
        password: 'admin',
        clientToken: Math.random(),
        code: '',
        loginType: 0
      },
      loginFormRules: {
        loginName: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 5, max: 16, message: '请输入正确账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入正确密码', trigger: 'blur' },
          { min: 5, max: 16, message: '请输入正确账号', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    change() {
      // 判断如果点击眼睛按钮为open的时候密码的password值去除
      this.passwordType === 'password' ? this.passwordType = '' : this.passwordType = 'password'
      // 用ref来绑定获取焦点
      this.$nextTick(() => {
        this.$refs.pwdInput.focus()
      })
    },
    async loginBtn() {
      try {
        const res = await this.$refs.loginForm.validate()
        console.log(res)
        this.loading = true
        await this.$store.dispatch('user/login', this.loginForm)
        await this.$store.dispatch('user/getUserInfo')

        this.$router.push('/')
      } catch (e) {
        console.log(e)
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #999;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;
      color:#999;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
width: 100%;
    height: 52px;
    margin-bottom: 24px;
    background: #fff;
    border: 1px solid #e2e2e2;
    border-radius: 4px;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  // min-height: 100%;
  // width: 100%;
  // background-color: $bg;
  // overflow: hidden;
  background-image: url('~@/assets/img/background.png');
  // background-position: center;
  position: relative;
    height: 100%;
    width: 100%;
    overflow: hidden;
    // background-image: url(../img/background.be4fae7d.png);
    background-repeat: no-repeat;
    background-size: cover;

  .login-form {
    position: absolute;
    width: 518px;
    height: 388px;
    top: 50%;
    left: 50%;
    margin-top: -194px;
    margin-left: -259px;
    padding: 76px 35px 0;
    background: #fff;
    -webkit-box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    border-radius: 10px;
    .login-logo{
    position: absolute;
    width: 96px;
    height: 96px;
    top: -46px;
    left: 50%;
    margin-left: -48px;
    }
}

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .login-btn{
    width: 100%;
    height: 52px;
    background: linear-gradient(262deg,#2e50e1,#6878f0);
    opacity: .91;
    border-radius: 8px;
    color: #fff;
    text-shadow: 0 7px 22px #cfcfcf;
  }
  .authCode{
    position: absolute;
    top: 229px;
    left: 353px;
  }
  .el-col-7 {
    width: 25%;
    height: 52px;
  }
}
</style>
